<?php

namespace App\Services\Sms;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;

class SmsService
{
    //
    public function sendSms($phoneNumbers,$templateCode, $templateParam){
        $client = $this->getClient();
        $request = $this->getRequest($phoneNumbers,$templateCode, $templateParam);
        $runtimeOptions = $this->getRuntimeOptions();
        $response = $client->sendSmsWithOptions($request, $runtimeOptions);
        return $response->body??null;
    }
    //
    public function getClient(){
        return new Dysmsapi($this->getConfig());
    }
    // 获取短信配置信息
    public function getConfig(): Config{
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => config('strator.dayu_sms.access_key_id'),
            // 您的AccessKey Secret
            "accessKeySecret" => config('strator.dayu_sms.access_key_secret')
        ]);
        // 访问的域名
        $config->endpoint = 'dysmsapi.aliyuncs.com';
        return $config;
    }
    //
    public function getRuntimeOptions():RuntimeOptions{
        $runtime                 = new RuntimeOptions();
        $runtime->maxIdleConns   = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout    = 10000;
        return $runtime;
    }
    //
    public function getRequest($phoneNumbers,$templateCode, $templateParam):SendSmsRequest{
        $request = new SendSmsRequest();
        $request->phoneNumbers = $phoneNumbers;
        $request->signName = config('strator.dayu_sms.sign_name');
        $request->templateCode = $templateCode;
        $request->templateParam = is_string($templateParam) ? $templateParam : json_encode($templateParam);
        return $request;
    }


}
